{% set poll = request.state.template_data.poll %}
{% if poll %}
    <form name="fpoll" action="{{ url_for('poll_update', po_id=poll.po_id) }}" onsubmit="return fpoll_submit(this);" method="post" target="win_poll">
        <input type="hidden" name="po_id" value="{{ poll.po_id }}">
        <input type="hidden" name="token" value="">
        <section id="poll">
            <header>
                <h2>调查</h2>
                {% if request.state.is_super_admin %}
                <a href="{{ url_for('poll_form_edit', po_id=poll.po_id) }}" class="btn_admin" title="调查的管理">
                    <i class="fa fa-cog fa-spin fa-fw"></i>
                    <span class="blind">调查的管理</span>
                </a>
                {% endif %}
                <a href="{{ url_for('poll_result', po_id=poll.po_id) }}" target="_blank" onclick="poll_result(this.href); return false;" class="btn_result"><i class="fa-solid fa-square-poll-horizontal"></i></a>
            </header>
            <div class="poll_con">
                <p>{{ poll.po_subject }}</p>
                <ul>
                    {% for i in range(1, 10) %}
                        {% set subject = getattr(poll, 'po_poll' ~ i, '') %}
                        {% if subject %}
                        <li class="chk_box">
                            <input type="radio" name="gb_poll" value="{{ i }}" id="gb_poll_{{ i }}">
                            <label for="gb_poll_{{ i }}">
                                <span>&nbsp;</span>
                                {{ subject }}
                            </label>
                        </li>
                        {% endif %}
                    {% endfor %}
                </ul>
                <div id="poll_btn">
                    <button type="submit" class="btn_poll">投票</button>
                </div>
            </div>
        </section>
    </form>
    <script>
        function fpoll_submit(f)
        {
            var chk = false;
            for (i=0; i<f.gb_poll.length;i ++) {
                if (f.gb_poll[i].checked == true) {
                    chk = f.gb_poll[i].value;
                    break;
                }
            }
        
            if (!chk) {
                alert("选择要投票的调查项目");
                return false;
            }

            f.token.value = generate_token();
            if (f.token.value == "") {
                alert("创建令牌失败.");
                return false;
            }
        
            var new_win = window.open("about:blank", "win_poll", "width=616,height=500,scrollbars=yes,resizable=yes");
            f.target = "win_poll";
        
            return true;
        }
        
        function poll_result(url)
        {
            win_poll(url);
        }
    </script>
{% endif %}